<?php

class SysController extends Admincontroller
{
    public function init()
    {
        parent::init();
        $this->_log = new LogModel();
    }
	//系统日志 author wuzhenhao data 2015-10-16
    public function logAction()
    {
        $this->_view->assign("title", '系统日志');
        $this->_view->assign("activetitle", '系统日志');
        $this->_view->assign("parentmenu", '系统管理');
        $this->_view->assign("action", '系统日志');
		
		if($this->getRequest()->isPost()){
			$search = $this->getRequest()->getPost('search');
			$limit = $this->getRequest()->getPost('limit');
			$offset = $this->getRequest()->getPost('offset');
			
			$logdata = $this->_log->selectAll($offset,$limit,$search);
			$returnarray = array(
					"total"=> $logdata['0'],
					"rows" => isset($logdata['1'])?$logdata['1']:[]
			);
			exit ( json_encode($returnarray) );
		}               
        $this->_view->display('sys/log');
    }
    //删除系统日志 author wuzhenhao data 2015-10-20
    public function dellogAction()
    {
    	$start = $this->getRequest()->getPost('start');
    	$end = $this->getRequest()->getPost('end');
    	if(empty($start)||empty($end)){
    		$returnarray = array('data' => '请选择开始,结束时间!' );
    		exit(json_encode($returnarray));
    	}
    	if($start>=$end){
    		$returnarray = array('data' => '开始时间不能大于等于结束时间!' );
    		exit(json_encode($returnarray));
    	}
    	$result = $this->_log->delete($start,$end);
    	if($result){
    		$returnarray = array('data' => '删除成功!' );
    		exit(json_encode($returnarray));
    	}else{
    		$returnarray = array('data' => '删除失败!' );
    		exit(json_encode($returnarray));
    	}
    	
    }
    
    //缓存清理 author wuzhenhao data 2015-10-15
    public function cleanAction()
    {	
    	$this->_view->assign("title", '缓存清理');
    	$this->_view->assign("activetitle", '缓存清理');
    	$this->_view->assign("parentmenu", '系统管理');
    	$this->_view->assign("action", '缓存清理');

    	$this->decache('../data/cache');
 
    	$this->_view->display('Sys/clean');
    }
    
    private function decache($dirname){
    	$dir = dir($dirname);
    	while($entry=$dir->read()){
    		if($entry!='.' && $entry!='..'){
    			if(is_dir($dirname.'/'.$entry)){
    				$this->decache($dirname. DIRECTORY_SEPARATOR . $entry);
    			}else{
    				unlink($dirname.'/'.$entry);     				
    			}
    		}
    	}
    	$dir->close();
    }
    //数据库操作 author wuzhenhao data 2015-10-15
    public function dbAction()
    {
    	$this->_view->assign("title", '数据库操作');
    	$this->_view->assign("activetitle", '数据库操作');
    	$this->_view->assign("parentmenu", '系统管理');
    	$this->_view->assign("action", '数据库操作');
    	$sqlArr = array();
    	$dir = dir('../data');
    	while($entry=$dir->read()){
    		if(preg_match('/^\d{14}\.sql$/', $entry)){
    			$sqlArr[] = $entry;
    		}
    	}
    	$dir->close();   	
    	$db = Yaf_Application::app()->getConfig();
    	$dbArr = $db -> mysql->read->toArray();
    	if ($this->getRequest()->isPost()) {
    		//备份   		
    		if($this->getRequest()->getPost('operate')=='backup'){
	    		$backtime = date('YmdHis',time());
	    		$command = "mysqldump -u {$dbArr['username']} -p{$dbArr['password']} -B shop > ../data/{$backtime}.sql" ;
	    		if(!$dbArr['password']){
	    			$returnarray = array('data' => '请先设置数据库密码!' );
	    			exit(json_encode($returnarray));
	    		}else{
	    			exec($command,$info,$flag);
					if($flag === 0){
						$returnarray = array('data' => '备份成功!' );
						exit(json_encode($returnarray));
					}else{
						$returnarray = array('data' => '备份失败!' );
						exit(json_encode($returnarray));
					}
	    		}   			
    		}else{
    			//还原
    			if($sqlArr[$this->getRequest()->getPost('num')]){
    				$command = "mysql -u {$dbArr['username']} -p{$dbArr['password']} shop < ../data/{$sqlArr[$this->getRequest()->getPost('num')]}" ;
    				exec($command,$info,$flag);
    				if($flag === 0){
						$returnarray = array('data' => '还原成功!' );
						exit(json_encode($returnarray));
					}else{
						$returnarray = array('data' => '还原失败!请先创建shop数据库' );
						exit(json_encode($returnarray));
					}    				
    			}else{
    				$returnarray = array('data' => '还原失败!' );
    				exit(json_encode($returnarray));
    			}
    		}    		
    	}
    	$this->_view->assign('sqlArr',$sqlArr);
    	$this->_view->display('Sys/db');
    }
    //删除备份 author wuzhenhao data 2015-10-15
    public function delAction(){
    	$sqlArr = array();
    	$dir = dir('../data');
    	while($entry=$dir->read()){
    		if(preg_match('/^\d{14}\.sql$/', $entry)){
    			$sqlArr[] = $entry;
    		}
    	}
    	$num = $this->getRequest()->getPost('num');
    	if($sqlArr[$num]){
    		$flag = unlink('../data/'.$sqlArr[$num]);
    		if($flag){
    			$returnarray = array('data' => '删除成功!' );
    			exit(json_encode($returnarray));
    		}else{
    			$returnarray = array('data' => '删除失败!' );
    			exit(json_encode($returnarray));
    		}
    	}else{
    		$returnarray = array('data' => '参数错误!' );
    		exit(json_encode($returnarray));
    	}
    }
}
